package cn.nextProject.controller;

import cn.nextProject.entity.FileResult;
import cn.nextProject.entity.Result;
import cn.nextProject.service.minio.MinioService;
import cn.nextProject.service.rayfile.BucketService;
import cn.nextProject.service.rayfile.FileService;
import io.minio.errors.*;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class RayfileController {

    @Autowired
    private BucketService bucketService;

    @Autowired
    private FileService fileService;

    @Autowired
    private MinioService minioService;

    /**
     * 查询某一目录中的文件
     * @return
     * @throws Exception
     */
    @GetMapping("/getFileList")
    public Result<?> getFileList(@RequestParam("buckName")String buckName,
                                 @RequestParam("pathName")String pathName) throws Exception {

        if (ObjectUtils.isEmpty(buckName)){
            return new Result<>().fail("请选择Bucket");
        }
        if (ObjectUtils.isEmpty(pathName)){
            pathName = null;
        }
        if ("".equals(pathName)){
            pathName = null;
        }
        List<FileResult> fileList = bucketService.getFileList(buckName, pathName);

        return new Result<>().success("查询成功",fileList);
    }

    /**
     * 创建文件夹
     * @param fileResult
     * @return
     * @throws Exception
     */
    @PostMapping("/createFolder")
    public Result<?> createFolder(@RequestBody FileResult fileResult) throws Exception {

        if (ObjectUtils.isEmpty(fileResult)){
            return new Result<>().fail("数据不能为空！");
        }

        if (ObjectUtils.isEmpty(fileResult.getBucketName())){
            return new Result<>().fail("请选择Bucket");
        }
        if (ObjectUtils.isEmpty(fileResult.getLastPath())){
            fileResult.setLastPath("");
        }
        if (ObjectUtils.isEmpty(fileResult.getName())){
            return new Result<>().fail("请输入文件夹名称");
        }

        Map<Integer, String> folder = fileService.createFolder(fileResult);

        String resultMsg = folder.get(1);
        if (ObjectUtils.isEmpty(resultMsg)){
            return new Result<>().fail(folder.get(0));
        }else{
            return new Result<>().success("创建成功",null);
        }

    }

    @PostMapping("/deleteFiles")
    public Result<?> deleteFiles(@RequestBody List<FileResult> fileResultList) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        if (CollectionUtils.isEmpty(fileResultList)){
            return new Result<>().fail("请选择文件");
        }

        minioService.deleteObjects(fileResultList);

        return new Result<>().success("删除成功",null);

    }

    @GetMapping("/renameFile")
    public Result<?> renameFile(@RequestParam("buckName")String buckName,
                                @RequestParam("oldPath")String oldPath,
                                @RequestParam("newPath")String newPath) throws Exception {

        if (ObjectUtils.isEmpty(buckName)){
            return new Result<>().fail("请选择Bucket");
        }
        if (ObjectUtils.isEmpty(oldPath)){
            return new Result<>().fail("请选择文件");
        }
        if (ObjectUtils.isEmpty(newPath)){
            return new Result<>().fail("请输入新的文件名");
        }
        Map<Integer, String> stringMap = fileService.renameFile(buckName, oldPath, newPath);
        if (stringMap.get(0) != null){
            return new Result<>().fail(stringMap.get(0));
        }else {
            return new Result<>().success("重命名成功",null);
        }


    }


}
